#include "stdafx.h"

TEST(container, map)
{
	map<string, int*> abs;
	map<string, int*>::iterator iter;
	int *a = new int(11);
	abs["d"] = a;
	abs["e"] = a;
	abs["x"] = a;
	abs["a"] = a;
	iter = abs.begin();
	while (iter != abs.end())
	{
		cout<<iter->first<<":"<<*(iter->second)<<endl;
		++iter;
	}
	*a = 14;
	iter = abs.begin();
	while (iter != abs.end())
	{
		cout<<iter->first<<":"<<*(iter->second)<<endl;
		++iter;
	}
	delete a;
	a = NULL;
	iter = abs.begin();
	while (iter != abs.end())
	{
		if (NULL == (iter->second))
		{
			cout<<"NULL"<<endl;
		}
		cout<<iter->first<<":"<<iter->second<<"-"<<*(iter->second)<<endl;
		++iter;
	}
}

TEST(map, find)
{
	map<string, int> tmp;
	tmp["a"] = 231;
	tmp["b"] = 232;
	EXPECT_EQ(2, tmp.size());
	cout<<tmp["c"]<<endl;
	EXPECT_EQ(3, tmp.size());

	EXPECT_EQ(tmp.end(), tmp.find("e"));
	EXPECT_EQ(3, tmp.size());
}

TEST(map, end)
{
	map<string, int> tmp;
	tmp["a"] = 231;
	tmp["b"] = 232;
	map<string, int>::iterator end = tmp.end();
	tmp["c"] = 233;
	EXPECT_EQ(end, tmp.end());
}

TEST(map, search)
{
	map<int, int> tmp;
	//insert find
	//500000 => 8000ms 9ms
	//1000000 => 17000ms 9ms
	//10000000 => 180000ms 9ms
	for (size_t i = 0; i < 500000; ++i)
	{
		tmp[i] = i;
	}
	__int64 start;
	cout<<"searching..."<<endl;
	start = timestamp->cur_micr_sec();
	for (size_t i = 0; i < 10000; i += 7)
	{
		tmp.find(i);
	}
	cout<<"used time:"<<timestamp->cur_micr_sec() - start <<" micr sec"<<endl;
}

//sort key and unsort value!!
TEST(multimap, is_sort)
{
	multimap<int, int> tmp;
	tmp.insert(pair<int, int>(1, 3));
	tmp.insert(pair<int, int>(1, 4));
	tmp.insert(pair<int, int>(3, 5));
	tmp.insert(pair<int, int>(2, 7));
	tmp.insert(pair<int, int>(1, 2));
	pair<multimap<int, int>::iterator, multimap<int, int>::iterator> ret;
	ret = tmp.equal_range(1);
	multimap<int, int>::iterator iter = ret.first;
	EXPECT_EQ(3, iter->second);
	++iter;
	EXPECT_EQ(4, iter->second);
	++iter;
	EXPECT_EQ(2, iter->second);

	//all
	iter = tmp.begin();
	EXPECT_EQ(3, iter->second);
	++iter;
	EXPECT_EQ(4, iter->second);
	++iter;
	EXPECT_EQ(2, iter->second);
	++iter;
	EXPECT_EQ(7, iter->second);
	++iter;
	EXPECT_EQ(5, iter->second);

}

TEST(multimap, bound)
{
	multimap<int, int> tmp;
	tmp.insert(pair<int, int>(1, 3));
	tmp.insert(pair<int, int>(1, 4));
	tmp.insert(pair<int, int>(3, 5));
	tmp.insert(pair<int, int>(2, 7));
	tmp.insert(pair<int, int>(1, 2));

	multimap<int, int>::iterator iter;
	iter = tmp.lower_bound(1);
	EXPECT_EQ(3, iter->second);

	iter = tmp.upper_bound(1);
	EXPECT_EQ(7, iter->second);//point to next
	--iter;//becareful the end()
	EXPECT_EQ(2, iter->second);

	iter = tmp.upper_bound(3);//point to end(next)
	EXPECT_EQ(tmp.end(), iter);

	iter = tmp.upper_bound(4);
	EXPECT_EQ(tmp.end(), iter);//point to end (not find)
}